package cn.skylark.wcm.service.impl;

import cn.skylark.wcm.dao.ConstantDao;
import cn.skylark.wcm.dao.TenanPropertyDao;
import cn.skylark.wcm.entity.Constant;
import cn.skylark.wcm.entity.Tenanproperty;
import cn.skylark.wcm.service.TenanPropertyService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TenanPropertyServiceImpl implements TenanPropertyService {
    /**
     * logger
     */
    private static final Logger LOGGER = LoggerFactory.getLogger( TenanPropertyServiceImpl.class );
    @Autowired
    public TenanPropertyDao tenanPropertyDao;
    @Autowired
    public ConstantDao constantDao;

    /**
     * 通过租户ID获取租户属性
     * 业务：通过id获取租户属性
     *
     * @param id
     * @return
     */
    @Override
    public List<Tenanproperty> getTenanPropertyByTenanId(Integer id) {
        LOGGER.info( "通过租户ID获取租户属性开始 租户id=" + id );
        List<Tenanproperty> list = tenanPropertyDao.getTenanPropertyByTenanId( id );
        LOGGER.info( "通过租户ID获取租户属性" );
        return list;
    }

    /**
     * 通过租户id获取租户属性
     * 业务:通过租户id获取租户属性
     *
     * @param tenanproperty
     * @return
     */
    @Override
    public Map<String, Object> getPropertyByTenanid(Tenanproperty tenanproperty) {
        LOGGER.info( "通过租户ID获取租户属性开始 租户id=" + tenanproperty.getTenanid() );
        Map<String, Object> map = new HashMap<>();
        Integer count = tenanPropertyDao.selectCount( tenanproperty );
        if (count > 0) {
            //获取租户所拥有的属性
            Example example = new Example( Tenanproperty.class );
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo( tenanproperty.getId() );
            List<Tenanproperty> list = tenanPropertyDao.selectByExample( example );
            //获取用户所拥有属性的keycode
            List<String> codelist = new ArrayList<>();
            for (Tenanproperty tenanpropertyMessage : list) {
                Constant constant = new Constant();
                constant.setKeycode( tenanpropertyMessage.getKeycode() );
                Constant constantMessage = constantDao.selectOne( constant );
                codelist.add( constantMessage.getKeycode() );
            }
            //获取所有属性
            Example exampleConStant = new Example( Constant.class );
            exampleConStant.setOrderByClause( "groupid asc" );
            List<Constant> listConStant = constantDao.selectByExample( exampleConStant );
            for (Constant constant : listConStant) {
                //判断keycode是否存在
                if (codelist.contains( constant.getKeycode() )) {
                    Tenanproperty tenanpropertyMessage = new Tenanproperty();
                    tenanpropertyMessage.setTenanid( tenanproperty.getTenanid() );
                    tenanpropertyMessage.setKeycode( constant.getKeycode() );
                    Tenanproperty tenanpropertyContext = tenanPropertyDao.selectOne( tenanpropertyMessage );
                    map.put( "" + constant.getKeyname() + "", tenanpropertyContext.getKeyvalue() + "|" + constant.getGroupid() );
                } else {
                    map.put( "" + constant.getKeyname() + "", "" + "|" + constant.getGroupid() );
                }
            }
        } else {
            //获取所有属性
            Example exampleConStant = new Example( Constant.class );
            exampleConStant.setOrderByClause( "groupid asc" );
            List<Constant> listConStant = constantDao.selectByExample( exampleConStant );
            for (Constant constant : listConStant) {
                map.put( "" + constant.getKeyname() + "", "" + "|" + constant.getGroupid() );
            }
        }

        LOGGER.info( "通过租户ID获取租户属性结束" );
        return map;
    }

    /**
     * 获取静态属性表
     * 业务：当在租户属性添加内容，点击下拉选的时候，
     *
     * @return constart内容返回
     * @Exception e 数据库连接异常数据
     */
    @Override
    public List<Constant> getConstant() {
        LOGGER.info( "获取静态属性表开始" );
        Example example = new Example( Constant.class );
        example.setOrderByClause( "groupid asc" );
        Example.Criteria criteria = example.createCriteria();
        List<Constant> list = constantDao.selectByExample( example );
        LOGGER.info( "获取静态属性表结束" );
        return list;
    }

    /**
     * 通过传入的tenanp的tenandi和keycode 查看该租户是否存在信息
     * 业务：count>0存在
     *
     * @param tenanproperty
     * @return
     */
    @Override
    public Integer selectTenanPropertyByKeycode(Tenanproperty tenanproperty) {
        LOGGER.info( "租户是否存在信息开始 租户信息" + tenanproperty );
        Integer count = tenanPropertyDao.selectCount( tenanproperty );
        LOGGER.info( "租户是否存在信息结束" );
        return count;
    }

    /**
     * 通过传入对象，查找tenanproperty对象
     * 业务:获取tenanproperty对象
     *
     * @param tenanproperty
     * @return
     */
    @Override
    public Tenanproperty selectTenanPropertyOne(Tenanproperty tenanproperty) {
        LOGGER.info( "查找tenanproperty对象开始  属相对象" + tenanproperty );
        Tenanproperty tenanpropertyid = tenanPropertyDao.selectOne( tenanproperty );
        LOGGER.info( "查找tenanproperty对象结束" );
        return tenanpropertyid;
    }

    /**
     * 保存属性(新增)
     * 业务:保存tenanproperty 对象信息
     *
     * @param tenanproperty
     * @return
     */
    @Override
    public void addTenanProperty(Tenanproperty tenanproperty, String keyvalue) {
        LOGGER.info( "保存属性(新增)开始 租户属性=" + tenanproperty + "属性值" + keyvalue );
        if (tenanproperty.getId() == null || "".equals( tenanproperty.getId() )) {
            tenanproperty.setKeyvalue( keyvalue );
            tenanPropertyDao.insert( tenanproperty );
        } else {
            Example example = new Example( Tenanproperty.class );
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo( "id", tenanproperty.getId() );
            tenanPropertyDao.deleteByExample( example );
            tenanproperty.setKeyvalue( keyvalue );
            tenanPropertyDao.insert( tenanproperty );
        }
        LOGGER.info( "保存属性(新增)结束" );
    }

    /**
     * 通过静态名获取静态表
     * 业务:通过静态表中的静态name获取静态表数据
     *
     * @param constant
     * @return
     */
    @Override
    public Constant getConstantByKeyName(Constant constant) {
        LOGGER.info( "通过静态名获取静态表开始 静态数据名称" + constant.getKeyname() );
        Constant constantMessage = constantDao.selectOne( constant );
        LOGGER.info( "通过静态名获取静态表结束" );
        return constantMessage;
    }
}

